package com.sqoq.downloader3.ui

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.Toast
import com.sqoq.downloader3.App
import com.sqoq.downloader3.MainActivity
import com.sqoq.downloader3.R
import com.sqoq.downloader3.base_ddg.BaseActivity_ddg
import com.sqoq.downloader3.databinding.ActivityFirstLunchDdgBinding
import com.sqoq.downloader3.getLanguageString_ddg
import com.sqoq.downloader3.lanuage_ddg.StringId_ddg
import com.tencent.mmkv.MMKV

class FirstLunchActivity_ddg : BaseActivity_ddg<ActivityFirstLunchDdgBinding>() {


    override val layout: Int
        get() = R.layout.activity_first_lunch_ddg

    override fun initView() {
        binding.cbBoxDdg.isSelected = true
        binding.cbBoxDdg.setOnClickListener {
            binding.cbBoxDdg.isSelected = binding.cbBoxDdg.isSelected.not()
        }
        binding.tvStartDdg.setOnClickListener {
            if (binding.cbBoxDdg.isSelected) {
                startActivity(MainActivity::class.java)
                MMKV.defaultMMKV().encode("firstopen_ddg", false)
                finish()
            } else {
                Toast.makeText(
                    this,
                    String.format(
                        getLanguageString_ddg(StringId_ddg.toastPrivacyPolicy_ddg),

                        getLanguageString_ddg(StringId_ddg.TermofUser_ddg),
                        getLanguageString_ddg(StringId_ddg.PrivacyPolicy_ddg),
                    ),
                    Toast.LENGTH_SHORT
                ).show()
            }

        }

        val builder = SpannableString(
            String.format(
                getLanguageString_ddg(StringId_ddg.agreedPrivacyPolicy_ddg),
                getLanguageString_ddg(StringId_ddg.TermofUser_ddg),
                getLanguageString_ddg(StringId_ddg.PrivacyPolicy_ddg),
            )
        )

        kotlin.runCatching {

            var text = getLanguageString_ddg(StringId_ddg.TermofUser_ddg)
            var a = builder.toString().indexOf(text)
            builder.setSpan(
                ForegroundColorSpan(Color.parseColor("#FF36AAAD")),
                a,
                a + text.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            builder.setSpan(
                Clickable_ddg() {
                    WebActivity_ddg.start(
                        baseContext,
                        App.TermofUser_ddg,
                        getLanguageString_ddg(StringId_ddg.TermofUser_ddg)
                    )
                }, a, a + text.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }

        binding.tvAgreenDdg.setMovementMethod(LinkMovementMethod.getInstance());
        kotlin.runCatching {
            var text = getLanguageString_ddg(StringId_ddg.PrivacyPolicy_ddg)
            var a = builder.toString().indexOf(text)

            builder.setSpan(
                ForegroundColorSpan(Color.parseColor("#FF36AAAD")),
                a,
                a + text.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            builder.setSpan(
                Clickable_ddg() {
                    WebActivity_ddg.start(
                        baseContext,
                        App.PrivacyPolicy_ddg,
                        getLanguageString_ddg(StringId_ddg.PrivacyPolicy_ddg)
                    )
                }, a, a + text.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        binding.tvAgreenDdg.setText(builder)
    }

    internal class Clickable_ddg(val clickListener: () -> Unit) : ClickableSpan(),
        View.OnClickListener {


        override fun onClick(v: View) {
            clickListener.invoke()
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.isUnderlineText = false
        }
    }
}